import Vue from 'vue'
import VueRouter from 'vue-router'
// 导入组件页面
import Home from '../components/system/Home.vue'

Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  // 路由的懒加载
  { path: '/login', component: () => import('../components/system/Login.vue') },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: () => import('../components/g6/Metro.vue')
      },
      {
        path: '/users',
        component: () => import('../components/user/Users.vue')
      },
      {
        path: '/authority',
        name: 'authority',
        component: () => import('../components/auth/Authority.vue')
      },
      {
        path: '/roles',
        name: 'roles',
        component: () => import('../components/auth/Roles.vue')
      }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转
  if (to.path === '/login') {
    return next()
  }

  const token = window.sessionStorage.getItem('Authorization')
  if (!token) {
    return next('/login')
  }

  next()
})

export default router
